package service;

import mapper.UserMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import pojo.UserInfo;
import utils.SqlSessionFactoryUtils;

import java.util.List;

public class UserInfoService {
    public List<UserInfo> selectAll(){
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = factory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        return mapper.selectAll();
    }

    public void updateStatus(Integer id, String status){
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = factory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.updateStatus(id,status);
//        更新和插入操作必须手动提交事务，否则更新无效
        sqlSession.commit();
    }

    public UserInfo seletUserInfo(String username){
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = factory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        UserInfo userInfo = mapper.selectUserInfo(username);
        return userInfo;
    }


}
